package com.bsc.rtc.lib.libraries.GilideDemo;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.bsc.rtc.lib.libraries.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.RequestOptions;

import java.util.concurrent.ExecutionException;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class GlideDemoActivity extends AppCompatActivity {
    String TAG = "GlideDemoActivity";
    ImageView imageView;
    RequestOptions sharedOptions;
    String url = "http://192.168.44.1:8765/images/upload/test1/ic_launcher.png";
    String thumUrl="http://hbimg.b0.upaiyun.com/b1b25bd5f3110ba879282be93cd6d080ce7e8ab4f8c0-9b6QQd_fw658";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_glide_demo);
        imageView = findViewById(R.id.test_image);
        initOptions();
        init();//前台加载
        //loadBackGround();//后台加载
        //loadByAppGlide();//loadByGlideApp
    }

    private void initOptions() {
        sharedOptions = new RequestOptions()
                .placeholder(R.mipmap.loading)
                .error(R.mipmap.error)
                .fallback(R.mipmap.fall)
                .fitCenter();
    }

    private void init() {
        int height = imageView.getMaxHeight();
        int width = imageView.getMaxWidth();
        //清除
        Glide.with(this).clear(imageView);
        //加载
        Glide.with(this)
                .load(url)
                .apply(sharedOptions)
                // .thumbnail(Glide.with(this).load(thumUrl))
                // .override(200,100)
                .into(imageView);
    }

    //后台加载
    private void loadBackGround() {
        Observable.create(new ObservableOnSubscribe<Bitmap>() {
            @Override
            public void subscribe(ObservableEmitter<Bitmap> emitter) throws Exception {
                FutureTarget<Bitmap> futureTarget =
                        Glide.with(GlideDemoActivity.this)
                                .asBitmap()
                                .load(url)
                                .submit();
                Bitmap bitmap = futureTarget.get();
                emitter.onNext(bitmap);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread()).
                subscribe(new Observer<Bitmap>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Bitmap bitmap) {
                        imageView.setImageBitmap(bitmap);
                        Glide.with(GlideDemoActivity.this).clear(imageView);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "err =" + e.getLocalizedMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    private void loadByAppGlide(){
        GlideApp.with(this)
                .load(url)
                .placeholder(R.mipmap.loading)
                .fitCenter()
                .into(imageView);
    }
}
